package com.jt.www.remote;

import com.jt.www.domain.vo.BmsCommissionVO;
import com.jt.www.domain.vo.BmsPaymentVO;
import com.jt.www.domain.vo.BmsSettlementVO;
import com.jt.www.model.correct.vo.CorrectionVoucherStatusVO;
import com.jt.www.model.reps.GenericListResponse;
import config.FeignConfiguration;
import feign.hystrix.FallbackFactory;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "bms-api", url = "${remote.bms_svc_url}",
        configuration = FeignConfiguration.class, fallbackFactory = BmsClient.FeignClientFallbackFactory.class)
public interface BmsClient {

    public static final Logger log = LoggerFactory.getLogger(BmsClient.class);

    /**
     * 佣金凭证查询接口
     *
     * @param pageNum
     * @param pageSize
     * @param policyNo
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/vouchers/commission/{channelCode}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<BmsCommissionVO> commission(@PathVariable("channelCode") String channelCode,
                                                    @RequestParam(value = "policyNo") String policyNo,
                                                    @RequestParam(value = "endorsementNo") String endorsementNo,
                                                    @RequestParam(value = "pageNum") Integer pageNum,
                                                    @RequestParam(value = "pageSize") Integer pageSize) throws Exception;

    /**
     * 解付状态查询接口
     *
     * @param channelCode
     * @param pageNum
     * @param pageSize
     * @param policyNo
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/vouchers/payment/{channelCode}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<BmsPaymentVO> payment(@PathVariable("channelCode") String channelCode,
                                              @RequestParam(value = "policyNo") String policyNo,
                                              @RequestParam(value = "endorsementNo") String endorsementNo,
                                              @RequestParam(value = "pageNum") Integer pageNum,
                                              @RequestParam(value = "pageSize") Integer pageSize) throws Exception;

    /**
     * 结算单查询接口
     *
     * @param channelCode
     * @param pageNum
     * @param pageSize
     * @param policyNo
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/vouchers/settlement/{channelCode}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<BmsSettlementVO> settlement(@PathVariable("channelCode") String channelCode,
                                                    @RequestParam(value = "policyNo") String policyNo,
                                                    @RequestParam(value = "endorsementNo") String endorsementNo,
                                                    @RequestParam(value = "pageNum") Integer pageNum,
                                                    @RequestParam(value = "pageSize") Integer pageSize) throws Exception;

    @ApiOperation(value = "冲正凭证状态查询接口", notes = "凭证状态查询")
    @GetMapping(value = "/corrections/correctVoucherStatus", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    GenericListResponse<CorrectionVoucherStatusVO> correctionVoucherStatus(@RequestParam("typeCode") String typeCode,
                                                                           @RequestParam("policyType") String policyType);

    @ApiOperation(value = "冲正凭证状态查询接口", notes = "凭证状态查询")
    @GetMapping(value = "/corrections/correctVoucherStatus", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    GenericListResponse<CorrectionVoucherStatusVO> correctVoucherStatus(@RequestParam("policyType") String policyType,
                                                                        @RequestParam("systemType") String systemType,
                                                                        @RequestParam("typeCode") String typeCode);

    /**
     * 获取业管跨期区间
     * @param fperiod
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/case/_bai/selectperiod?fperiod={fperiod}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse getIntertemporal(@PathVariable("fperiod") String fperiod)throws Exception;

    @Component
    class FeignClientFallbackFactory implements FallbackFactory<BmsClient> {

        @Override
        public BmsClient create(Throwable cause) {
            return new BmsClient() {

                @Override
                public GenericListResponse<BmsCommissionVO> commission(String channelCode, String policyNo, String endorsementNo, Integer pageNum, Integer pageSize) throws Exception {
                    log.error("佣金凭证查询接口查询失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<BmsPaymentVO> payment(String channelCode, String policyNo, String endorsementNo, Integer pageNum, Integer pageSize) throws Exception {
                    log.error("解付状态查询接口查询失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<BmsSettlementVO> settlement(String channelCode, String policyNo, String endorsementNo, Integer pageNum, Integer pageSize) throws Exception {
                    log.error("结算单查询接口查询失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<CorrectionVoucherStatusVO> correctionVoucherStatus(String typeCode, String policyType) {
                    log.error("{}-调用业管失败,fallback reason:", "correctionVoucherStatus", cause);
                    return null;
                }

                @Override
                public GenericListResponse<CorrectionVoucherStatusVO> correctVoucherStatus(String policyType, String systemType, String typeCode) {
                    log.error("{}-调用业管失败,fallback reason:", "correctionVoucherStatus", cause);
                    return null;
                }

                @Override
                public GenericListResponse getIntertemporal(String fperiod) throws Exception {
                    log.error("获取业管跨期日期失败,fallback reason:",cause);
                    return null;
                }
            };
        }
    }
}
